package Listening;


import java.io.IOException;
import java.nio.file.Path;

import Commnads.ListenCommands;

/**
 * class Listening's task is to listen to the OS, and to execute the 
 * requested commands via the {@link Listener} by the {@link Observer}.
 * This class contained also the {@link Tagger}, that executed the initialization thread. 
 * @author DROR
 */

public class Listening 
{
	private Listener listener;
	private Tagger tagger;
	private ListenCommands listencommands;

	/**
	 * Constructor
	 * @param listencommands reference of class {@link ListenCommands}
	 * @throws IOException
	 */
	public Listening(ListenCommands listencommands) throws IOException 
	{
		this.listencommands= listencommands;
		listener= new Listener(listencommands);
		tagger= new Tagger(listencommands);
	}

	/**
	 * This function adding a new directory to listening (via the {@link Listener})
	 * @param dir the {@link Path} of the directory
	 * @throws IOException
	 */
	public void addDir(Path dir) throws IOException 
	{
		listener.addDir(dir);
	}

	/**
	 * This function stopping the listening (via the {@link Listener})
	 */
	public void stopListening() 
	{
		if (listener.isListeningFlag())
		{
			listener.insertToLog("The listening had stopped");
			listener.stopListening();
		}
	}

	/**
	 * This function calculate the percent of the tagged files,
	 * for updating the progress bar (via the {@link Tagger}) 
	 * @return The percent
	 */
	public int getPercent()
	{
		return tagger.getPercent();
	}

	/**
	 * This function remove directory from listening (via the {@link Listener})
	 * @param dir The {@link Path} of directory
	 */
	public void removeDir(Path dir) 
	{
		listener.removeDir(dir);
	}

	/**
	 * This function remove all directories from listening (via the {@link Listener})
	 */
	public void removeAllDirs() 
	{
		listener.removeAllDirs();
	}

	/**
	 * This function starting the listening (via the {@link Listener})
	 */
	public void startListening() 
	{

		if (!listener.isListeningFlag())
		{
			listener.insertToLog("The listening had started");
			listener.startListening();
			new Thread(listener).start();
		}
	}

	/**
	 * This function starting the initialization thread (via the {@link Tagger})
	 */
	public void startInitialization() 
	{
		if (!tagger.getStartInit())
		{
			listener.insertToLog("The initialization had started");
			tagger = new Tagger(listencommands);
			tagger.setStartInit(true);
			tagger.start();
		}
	}

	/**
	 * This function stopping the initialization thread (via the {@link Tagger})
	 */
	public void stopInitialization() 
	{
		if (tagger.getStartInit())
		{
			listener.insertToLog("The initialization had stopped");
			tagger.setStartInit(false);
		}
	}

	/**
	 * This function pausing the initialization thread (via the {@link Tagger})
	 */
	public void pauseInitialization() 
	{
		if (!tagger.isPause())
		{
			listener.insertToLog("The initialization had paused");
			tagger.pauseInit();
		}
	}

	/**
	 * This function resuming the initialization thread (via the {@link Tagger})
	 */
	public void resumeInitialization() 
	{
		if (tagger.isPause())
		{
			listener.insertToLog("The initialization had resumed");
			tagger.resumeInit();
		}
	}
}
